package com.yun.zf.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.app.mapper.ZfFollowMapper;
import com.yun.zf.app.service.ZfCommunityService;
import com.yun.zf.app.service.ZfFollowService;
import com.yun.zf.app.service.ZfHouseBasicService;
import com.yun.zf.common.admin.model.house.HouseListMo;
import com.yun.zf.common.app.domain.ZfFollow;
import com.yun.zf.common.app.dto.detail.CommunityNearByDto;
import com.yun.zf.common.app.dto.follow.FollowDto;
import com.yun.zf.common.app.dto.follow.FollowInfoDto;
import com.yun.zf.common.app.model.home.FollowMo;
import com.yun.zf.common.mapstruct.app.FollowMapper;
import com.yun.zf.system.redis.RedisCache;
import com.yun.zf.system.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 房源关注表 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-05-27
 */
@Service
public class ZfFollowServiceImpl extends ServiceImpl<ZfFollowMapper, ZfFollow> implements ZfFollowService {

    @Autowired
    private ZfFollowService _this;
    @Autowired
    private ZfHouseBasicService basicService;
    @Autowired
    RedisCache redisCache;
    @Autowired
    private ZfCommunityService communityService;

    @Override
    public int addFollow(FollowMo followMo) {
        ZfFollow follow = FollowMapper.INSTANCE.moToFollow(followMo);
        Long userId = SecurityUtils.getLoginUser().getUserId();
        follow.setUserId(userId);
        follow.setCreateTime(new Date());
        int count = baseMapper.insert(follow);
        if (count > 0) {
            redisCache.deleteObject(new String[]{"follow::" + follow.getUserId() + "_" + follow.getFollowType()});
        }
        return count;
    }

    @Override
    public List<FollowInfoDto> myFollow(Long userId, int type) {
        //1、 查询用户关注的数据所在城市
        List<FollowDto> clist = baseMapper.getFollowCitys(userId, type);
        if (clist != null || clist.size() != 0) {
            List<FollowInfoDto> infoDtos = new ArrayList<>();
            for (FollowDto f : clist
            ) {
                // 根据用户及城市查询当前关注的房源id 集合
                List<Long> ids = baseMapper.getIds(userId, f.getProvinceCode(), f.getCityCode());
                FollowInfoDto followInfoDto = new FollowInfoDto();
                followInfoDto.setCityName(f.getCityName());
                if (type == 3) {
                    List<CommunityNearByDto> communityNearByDtoList = communityService.getBasicCommunity(ids);
                    followInfoDto.setCommunityNearByDtoList(communityNearByDtoList);
                } else {
                    List<HouseListMo> cityHouse = basicService.houseBasic(f.getProvinceCode(), f.getCityCode(), ids);
                    followInfoDto.setHouseList(cityHouse);
                }
                infoDtos.add(followInfoDto);
            }
            return infoDtos;
        }
        return null;
    }

    @Override
    public int delFollow(Long userId, List<Long> horcIds, int type) {
        QueryWrapper<ZfFollow> followQueryWrapper = new QueryWrapper<>();
        followQueryWrapper.lambda().in(ZfFollow::getHorcId, horcIds).eq(ZfFollow::getUserId, userId);
        int count = baseMapper.delete(followQueryWrapper);
        if (count > 0) {
            redisCache.deleteObject(new String[]{"follow::" + userId + "_" + type});
        }
        return count;
    }

    @Override
    public int hasFollow(Long userId ,Long horcId) {
        QueryWrapper<ZfFollow> followQueryWrapper = new QueryWrapper<>();
        followQueryWrapper.lambda().in(ZfFollow::getHorcId, horcId).eq(ZfFollow::getUserId, userId);
        return baseMapper.delete(followQueryWrapper);
    }
}
